package net.shopxx.action.admin;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;
  
import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.entity.Admin;
import net.shopxx.entity.DeliveryCorp;
import net.shopxx.entity.DeliveryItem;
import net.shopxx.entity.DeliveryType;
import net.shopxx.entity.OrderItem;
import net.shopxx.entity.Payment;
import net.shopxx.entity.PaymentConfig;
import net.shopxx.entity.PrologOrder;
import net.shopxx.entity.Refund;
import net.shopxx.entity.Reship;
import net.shopxx.entity.Shipping;
import net.shopxx.entity.Payment.PaymentType;
import net.shopxx.entity.Product.WeightUnit;
import net.shopxx.entity.Refund.RefundType;
import net.shopxx.service.AdminService;
import net.shopxx.service.DeliveryCorpService;
import net.shopxx.service.DeliveryTypeService;
import net.shopxx.service.PaymentConfigService;
import net.shopxx.service.ProduceOrderService;
import net.shopxx.service.PrologOrderService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;

/**
 * 后台Action类 - 生产物流
 */

@ParentPackage("admin")
public class PrologOrderAction extends BaseAdminAction {

	private static final long serialVersionUID = 7656520378191246544L;
	private PrologOrder prologOrder;
	private Payment payment;
	private Shipping shipping;
	private Refund refund;
	private Reship reship;

	private List<OrderItem> orderItemList;
	
	private List produceLoadList;
	
	private List deliveryLoadList;
	
	private List<DeliveryItem> deliveryItemList;
	@SuppressWarnings("unused")
	private List<Admin> produceOrderList;
	@SuppressWarnings("unused")
	private List<Admin> deliveryOrderList;
	@Resource
	private DeliveryTypeService deliveryTypeService;
	@Resource
	private DeliveryCorpService deliveryCorpService;
	@Resource
	private PaymentConfigService paymentConfigService;

	@Resource
	private ProduceOrderService produceOrderService;	
	
	@Resource
	private PrologOrderService prologOrderService;	
	
	@Resource
	private AdminService adminService;	

   //生产物流单列表
	public String list() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
			
		pager = prologOrderService.findByPager(pager,detachedCriteria);
		return LIST;
	}

//	// 删除
//	public String delete() {
//		try {
//			
//			for(int i = 0; i < ids.length; i ++) {
//				PrologOrder persistent = prologOrderService.load(ids[i]);
//				persistent.setIsEnabled(false);
//				prologOrderService.update(persistent);
//			}
//			
//		} catch (RuntimeException e) {
//			return ajaxJsonErrorMessage("删除失败，数据被引用！");
//		}
//		
//		return ajaxJsonSuccessMessage("删除成功！");
//		
//	}
//	
//	
//	
//	
//
//	// 收款
//	public String inputInfo() {	
//		prologOrder = prologOrderService.load(id);
//		setResponseNoCache();
//		return "input";
//	}	
//	
//	
//	// 分配详情     
//	public String input() {	
//		setResponseNoCache();
//		 
//		PrologOrder persistent = prologOrderService.load(id);
//		
//		persistent.setIsState("5");
//		
//		persistent.setPayee(prologOrder.getPayee());
//		
//		persistent.setPayer(prologOrder.getPayer());
//		
//		persistent.setPaymentStatus(prologOrder.getPaymentStatus());
//		
//		persistent.setTotalAmount(prologOrder.getTotalAmount());
//		
//		persistent.setPaymentFee(prologOrder.getPaymentFee());
//		
//		
//		persistent.setModifyDate(new Date());
//		
//		persistent.setDeliveryCompTime(new Date());
//		
//		prologOrderService.update(persistent);
//	
//		redirectionUrl = "prolog_order!list.action";
//		return SUCCESS;
//	}
	
	// 分配
	public String produceDistrib() {	
		setResponseNoCache();
		return "produceDistrib";
	}
	
	// 分配详情     
	public String produceinfo() {	
		setResponseNoCache();
		 
		PrologOrder persistent = prologOrderService.load(id);
		
		 
		
		persistent.setProduceDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));

		persistent.setProduceDistribStaff(prologOrder.getProduceDistribStaff());
		
		persistent.setProduceDistribTime(new Date());
		
		persistent.setModifyDate(new Date());
		
		persistent.setIsState("2");
		prologOrderService.update(persistent);
	
		redirectionUrl = "prolog_order!list.action";
		return SUCCESS;
	}
	
	// 完成
	public String complete() {
	

				PrologOrder persistent = prologOrderService.load(id);
				persistent.setIsState("3");
				prologOrderService.update(persistent);

				redirectionUrl = "prolog_order!list.action";
				return SUCCESS;
	}
	
	
//	// 分配
//	public String deliveryDistrib() {	
//		setResponseNoCache();
//		return "deliveryDistrib";
//	}
//	
//	// 分配详情     
//	public String deliveryinfo() {	
//		setResponseNoCache();
//		 
//		PrologOrder persistent = prologOrderService.load(id);
//		
//		 
//		
//		persistent.setDeliveryDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));
//
//		persistent.setDeliveryDistribStaff(prologOrder.getDeliveryDistribStaff());
//		
//		persistent.setDeliveryDistribTime(new Date());
//		
//		persistent.setModifyDate(new Date());
//		
//		persistent.setIsState("4");
//		prologOrderService.update(persistent);
//	
//		redirectionUrl = "prolog_order!list.action";
//		return SUCCESS;
//	}
//	
//	
//	// 退单
//	public String back() {
//		try {
//
//				PrologOrder persistent = prologOrderService.load(id);
//				persistent.setIsState("6");
//				prologOrderService.update(persistent);
//
//		} catch (RuntimeException e) {
//			return ajaxJsonErrorMessage("退单失败，数据被引用！");
//		}
//		
//		return ajaxJsonSuccessMessage("退单成功！");
//	}
	
	//生产人员工作负荷列表
	public String produceWorkLoad() {
		
		produceLoadList = prologOrderService.getProduceLoadByDistrib();		
		return "produceload";
	}
	
	
//	//生产人员工作负荷列表
//	public String deliveryWorkLoad() {
//		
//		deliveryLoadList = prologOrderService.getDeliveryLoadByDistrib();		
//		return "deliveryload";
//	}
	
	
	//未分配生产列表
	
	public String wplist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "1"));	
			
		pager = prologOrderService.findByPager(pager,detachedCriteria);
		return "wplist";
	}

	
	
	// 分配
	public String wpProduceDistrib() {	
		setResponseNoCache();
		return "wpproduceDistrib";
	}
	
	
	// 分配详情     
	public String wpProduceinfo() {	
		setResponseNoCache();
		 
		PrologOrder persistent = prologOrderService.load(id);
		
		 
		
		persistent.setProduceDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));

		persistent.setProduceDistribStaff(prologOrder.getProduceDistribStaff());
		
		persistent.setProduceDistribTime(new Date());
		
		persistent.setModifyDate(new Date());
		
		persistent.setIsState("2");
		prologOrderService.update(persistent);
	
		redirectionUrl = "prolog_order!wplist.action";
		return SUCCESS;
	}
	
	// 删除
	public String wpdelete() {
		try {
			
			for(int i = 0; i < ids.length; i ++) {
				PrologOrder persistent = prologOrderService.load(ids[i]);
				persistent.setIsEnabled(false);
				prologOrderService.update(persistent);
			}

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("批量作废失败，数据被引用！");
		}		
		return ajaxJsonSuccessMessage("批量作废成功！");
	}
	

	// 作废
	public String invalid() {
		try {

				PrologOrder persistent = prologOrderService.load(id);
				persistent.setIsEnabled(false);
				prologOrderService.update(persistent);

		} catch (RuntimeException e) {
			return ajaxJsonErrorMessage("作废失败，数据被引用！");
		}
		
		return ajaxJsonSuccessMessage("作废成功！");
	}
	
	
	//已分配生产列表
	public String yplist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "2"));	
			
		pager = prologOrderService.findByPager(pager,detachedCriteria);
		return "yplist";
	}

	
	
	// 分配
	public String ypProduceDistrib() {	
		setResponseNoCache();
		return "ypproduceDistrib";
	}
	
	
	// 分配详情     
	public String ypProduceinfo() {	
		setResponseNoCache();
		 
		PrologOrder persistent = prologOrderService.load(id);
		
		 
		
		persistent.setProduceDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));

		persistent.setProduceDistribStaff(prologOrder.getProduceDistribStaff());
		
		persistent.setProduceDistribTime(new Date());
		
		persistent.setModifyDate(new Date());
		
	
		prologOrderService.update(persistent);
	
		redirectionUrl = "prolog_order!yplist.action";
		return SUCCESS;
	}
		
	// 完成
	public String ypcomplete() {	
				PrologOrder persistent = prologOrderService.load(id);
				persistent.setIsState("3");
				prologOrderService.update(persistent);

				redirectionUrl = "prolog_order!yplist.action";
				return SUCCESS;
	}
	
	
	
	//库存单列表
	public String klist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "3"));	
		detachedCriteria.add(Restrictions.eq("isDelState", "1"));
			
		pager = prologOrderService.findByPager(pager,detachedCriteria);
		return "klist";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
//	//未分配物流列表
//
//	
//	public String wdlist() {
//		
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
//		detachedCriteria.add(Restrictions.eq("isEnabled", true));
//		detachedCriteria.add(Restrictions.eq("isState", "3"));	
//			
//		pager = prologOrderService.findByPager(pager,detachedCriteria);
//		return "wdlist";
//	}
//
//	
//	
//	// 分配
//	public String wdDeliveryDistrib() {	
//		setResponseNoCache();
//		return "wddeliveryDistrib";
//	}
//	
//	
//	// 分配详情     
//	public String wdDeliveryinfo() {	
//		setResponseNoCache();
//		 
//		PrologOrder persistent = prologOrderService.load(id);
//		
//		 
//		
//		persistent.setDeliveryDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));
//
//		persistent.setDeliveryDistribStaff(prologOrder.getDeliveryDistribStaff());
//		
//		persistent.setDeliveryDistribTime(new Date());
//		
//		persistent.setModifyDate(new Date());
//		
//		persistent.setIsState("4");
//		prologOrderService.update(persistent);
//	
//		redirectionUrl = "prolog_order!wdlist.action";
//		return SUCCESS;
//	}
//	
//	// 删除
//	public String wddelete() {
//		try {
//			
//			for(int i = 0; i < ids.length; i ++) {
//				PrologOrder persistent = prologOrderService.load(ids[i]);
//				persistent.setIsEnabled(false);
//				prologOrderService.update(persistent);
//			}
//
//		} catch (RuntimeException e) {
//			return ajaxJsonErrorMessage("删除失败，数据被引用！");
//		}		
//		return ajaxJsonSuccessMessage("删除成功！");
//	}
//	
//	
//	
//	
//	
//	
//	
//	
//	
//	//已分配物流列表
//	
//	public String ydlist() {
//		
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
//		detachedCriteria.add(Restrictions.eq("isEnabled", true));
//		detachedCriteria.add(Restrictions.eq("isState", "4"));	
//			
//		pager = prologOrderService.findByPager(pager,detachedCriteria);
//		return "ydlist";
//	}
//
//	
//	
//	// 分配
//	public String ydDeliveryDistrib() {	
//		setResponseNoCache();
//		return "yddeliveryDistrib";
//	}
//	
//	
//	// 分配详情     
//	public String ydDeliveryinfo() {	
//		setResponseNoCache();
//		 
//		PrologOrder persistent = prologOrderService.load(id);
//		
//		 
//		
//		persistent.setDeliveryDistribManager(((String) getSession("SPRING_SECURITY_LAST_USERNAME")));
//
//		persistent.setDeliveryDistribStaff(prologOrder.getDeliveryDistribStaff());
//		
//		persistent.setDeliveryDistribTime(new Date());
//		
//		persistent.setModifyDate(new Date());
//		
//		prologOrderService.update(persistent);
//	
//		redirectionUrl = "prolog_order!ydlist.action";
//		return SUCCESS;
//	}
//	
//	// 删除
//	public String yddelete() {
//		try {
//			
//			for(int i = 0; i < ids.length; i ++) {
//				PrologOrder persistent = prologOrderService.load(ids[i]);
//				persistent.setIsEnabled(false);
//				prologOrderService.update(persistent);
//			}
//
//		} catch (RuntimeException e) {
//			return ajaxJsonErrorMessage("删除失败，数据被引用！");
//		}		
//		return ajaxJsonSuccessMessage("删除成功！");
//	}
//	
//	
//	// 收款
//	public String ydinputInfo() {	
//		prologOrder = prologOrderService.load(id);
//		setResponseNoCache();
//		return "ydinput";
//	}	
//	
//	
//	// 分配详情     
//	public String ydinput() {	
//		setResponseNoCache();
//		 
//		PrologOrder persistent = prologOrderService.load(id);
//		
//		persistent.setIsState("5");
//		
//		persistent.setPayee(prologOrder.getPayee());
//		
//		persistent.setPayer(prologOrder.getPayer());
//		
//		persistent.setPaymentStatus(prologOrder.getPaymentStatus());
//		
//		persistent.setTotalAmount(prologOrder.getTotalAmount());
//		
//		persistent.setPaymentFee(prologOrder.getPaymentFee());
//		
//		
//		persistent.setModifyDate(new Date());
//		
//		persistent.setDeliveryCompTime(new Date());
//		
//		prologOrderService.update(persistent);
//	
//		redirectionUrl = "prolog_order!ydlist.action";
//		return SUCCESS;
//	}
//	
//	//退单列表
//	public String tlist() {
//		
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
//		detachedCriteria.add(Restrictions.eq("isEnabled", true));
//		detachedCriteria.add(Restrictions.eq("isState", "6"));	
//			
//		pager = prologOrderService.findByPager(pager,detachedCriteria);
//		return "tlist";
//	}
//	
	
	//废单列表
	
	public String flist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", false));
		pager = prologOrderService.findByPager(pager,detachedCriteria);
		return "flist";
	}
	
	
//	//恢复生产单
//	public String recovery() {
//			PrologOrder persistent = prologOrderService.load(id);
//			persistent.setIsEnabled(true);
//			prologOrderService.update(persistent);
//	        redirectionUrl = "prolog_order!flist.action";
//	        return SUCCESS;
//	}
	
	
	
	
	
	//已完成列表
	public String oklist() {
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PrologOrder.class);
		detachedCriteria.add(Restrictions.eq("isEnabled", true));
		detachedCriteria.add(Restrictions.eq("isState", "3"));	
			
		pager = prologOrderService.findByPager(pager,detachedCriteria);
		return "oklist";
	}
 

	
	
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}
	
	// 获取所有重量单位
	public List<WeightUnit> getAllWeightUnit() {
		List<WeightUnit> allWeightUnit = new ArrayList<WeightUnit>();
		for (WeightUnit weightUnit : WeightUnit.values()) {
			allWeightUnit.add(weightUnit);
		}
		return allWeightUnit;
	}
	
	// 获取所有配送方式
	public List<DeliveryType> getAllDeliveryType() {
		return deliveryTypeService.getAll();
	}
	
	// 获取所有支付方式
	public List<PaymentConfig> getAllPaymentConfig() {
		return paymentConfigService.getAll();
	}
	
	// 获取支付类型（不包含在线充值）
	public List<PaymentType> getNonRechargePaymentTypeList() {
		List<PaymentType> paymentTypeList = new ArrayList<PaymentType>();
		for (PaymentType paymentType : PaymentType.values()) {
			if (paymentType != PaymentType.recharge) {
				paymentTypeList.add(paymentType);
			}
		}
		return paymentTypeList;
	}
	
	// 获取退款类型
	public List<RefundType> getRefundTypeList() {
		List<RefundType> refundTypeList = new ArrayList<RefundType>();
		for (RefundType refundType : RefundType.values()) {
			refundTypeList.add(refundType);
		}
		return refundTypeList;
	}
	
	// 获取所有物流公司
	public List<DeliveryCorp> getAllDeliveryCorp() {
		return deliveryCorpService.getAll();
	}




	public ProduceOrderService getProduceOrderService() {
		return produceOrderService;
	}


	public void setProduceOrderService(ProduceOrderService produceOrderService) {
		this.produceOrderService = produceOrderService;
	}


	public Payment getPayment() {
		return payment;
	}

	public void setPayment(Payment payment) {
		this.payment = payment;
	}

	public Shipping getShipping() {
		return shipping;
	}

	public void setShipping(Shipping shipping) {
		this.shipping = shipping;
	}

	public Refund getRefund() {
		return refund;
	}

	public void setRefund(Refund refund) {
		this.refund = refund;
	}

	public Reship getReship() {
		return reship;
	}

	public void setReship(Reship reship) {
		this.reship = reship;
	}

	public List<OrderItem> getOrderItemList() {
		return orderItemList;
	}

	public void setOrderItemList(List<OrderItem> orderItemList) {
		this.orderItemList = orderItemList;
	}

	public List<DeliveryItem> getDeliveryItemList() {
		return deliveryItemList;
	}

	public void setDeliveryItemList(List<DeliveryItem> deliveryItemList) {
		this.deliveryItemList = deliveryItemList;
	}





	public List<Admin> getProduceOrderList() {
		return adminService.getAll();
	}

	public void setProduceOrderList(List<Admin> produceOrderList) {
		this.produceOrderList = produceOrderList;
	}


	public PrologOrder getPrologOrder() {
		return prologOrder;
	}


	public void setPrologOrder(PrologOrder prologOrder) {
		this.prologOrder = prologOrder;
	}


	public PrologOrderService getPrologOrderService() {
		return prologOrderService;
	}


	public void setPrologOrderService(PrologOrderService prologOrderService) {
		this.prologOrderService = prologOrderService;
	}


	public List getProduceLoadList() {
		return produceLoadList;
	}


	public void setProduceLoadList(List produceLoadList) {
		this.produceLoadList = produceLoadList;
	}


	public List getDeliveryLoadList() {
		return deliveryLoadList;
	}


	public void setDeliveryLoadList(List deliveryLoadList) {
		this.deliveryLoadList = deliveryLoadList;
	}

	public List<Admin> getDeliveryOrderList() {
		return adminService.getAll();
	}

	public void setDeliveryOrderList(List<Admin> deliveryOrderList) {
		this.deliveryOrderList = deliveryOrderList;
	}




}